package com.donglan.pethome.organization.controller;

import com.donglan.pethome.organization.domain.Department;
import com.donglan.pethome.organization.query.DepartmentQuery;
import com.donglan.pethome.organization.service.DepartmentService;
import com.donglan.pethome.basic.util.AjaxResult;
import com.donglan.pethome.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: zhangdonghui
 * @Date: 2021/10/25 10:32
 */
@RequestMapping("department")
@RestController//@RestController 它是一个组合注解        @RestController =@Controller+@ResponseBody
//@CrossOrigin // 必须是Spring4.2.5以上的版本才能用
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 保存与修改部门
     *
     * 快速生成try catch  Ctrl+Alt+T
     * @param department
     * @return AjaxResult
     */
    @PutMapping// @PutMapping = @RequestMapping(method = RequestMethod.PUT)
    public AjaxResult addorUpdate(@RequestBody Department department){
        try {
            departmentService.addorUpdate(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }


    /**
     * 根据id删除数据
     * @param id
     * @return AjaxResult
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除数据
     * @param departments 部门集合
     * @return
     */
    @PatchMapping("batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            departmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 根据id查询对象
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Department loadById(@PathVariable("id")Long id){
        return departmentService.loadById(id);
    }


    /**
     * 查询分页列表的部门对象
     * @return
     */
    @PatchMapping
    public PageList<Department> loadPageByQuery(@RequestBody DepartmentQuery query){
        //return departmentService.loadAll();
        return departmentService.loadPageByQuery(query);
    }

    /**
     * 查询所有的部门列表
     * @return
     */
    @PatchMapping("/loadAll")
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }
}
